package com.maya.medicalwastes.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;

import com.maya.medicalwastes.entity.UpdateInfoEntity;


/**
 * *
 */
public class AppUtil {

    public static int getVersionCode(Context context) {
        int versionCode = 0;
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    public static String getVersionName(Context context) {
        String versionName = "";
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public static String getVersionData(Context context) {
        String versionData = "V" + getVersionName(context) + "(" + getVersionCode(context) + ")";
        return versionData;
    }

    //看是否需要更新
    public static boolean isNeedUpdate(UpdateInfoEntity updateInfo, Context context) {
        int new_version = updateInfo.getVersionCode(); // 最新版本的版本号
        // 获取当前版本号
        int now_version = 0;
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            now_version = packageInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (new_version <= now_version) {
            return false;
        } else {
            return true;
        }
    }


    public static String getMobilType() {
        String model = Build.MODEL;
        model = model.replaceAll(" ", "");
        model = model.toLowerCase();
        return model;
    }
}
